// pages/regist/regist.js
const md = require('../../utils/md5.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gou:'',
    gousign:1,
    backcolor:0,
    msg:'获取',
    count: 60,
    auth:1,
    isauth:0,
    num:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.type);
    var type = options.type;
    if(type == 'forget'){
      wx.setNavigationBarTitle({
        title: '忘记密码',
      })
      this.setData({
        auth:0
      })
    }
  },
  onShow:function(){
    var auth = wx.getStorageSync('auth');
    if(auth ==1){
      this.setData({
        gousign: 2,
        gou: '✔',
        backcolor: 1
      })
    }
  },
  beagree:function(){
    console.log(this.data.gousign);
    if(this.data.gousign==1){
      this.setData({
        gousign: 2,
        gou:'✔',
        backcolor:1
      })
    }else{
      this.setData({
        gousign: 1,
        gou: '',
        backcolor: 0
      })
    }
  },
  getmsg:function(){
    var that = this
    // console.log(1);
    this.setData({
      num: that.data.num+1
    })
    if(that.data.num == 2){
      // 获取时间戳
      var apinameIsMeb = "/Action/PK01/SMSVerifySend";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      console.log({
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        mobile: that.data.phone
      });
      // return;
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          mobile:that.data.phone
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
        }
      })
    }
    if(that.data.count==0){
      this.setData({
        num:1
      })
      return;
    }else{
      var interval = setInterval(function () {
        if (that.data.count == 0){
          clearInterval(interval);
          that.setData({
            msg: '获取',
            count:60,
            num: 1
          })
          return true;
        }
        that.setData({
          msg: that.data.count - 1 + 's',
          count: that.data.count - 1
        })
      }, 1000)
    }
  },
  formSubmit: function (e) {
    var that =this;
    var dataform=e.detail.value;
   
    if (dataform.phone == '' || dataform.phone.length !=11){
      wx.showModal({
        title: '',
        content: '手机号码不能为空或格式不对',
      })
      return;
    }
    if(dataform.authword =='' || dataform.authword.length !=4){
      wx.showModal({
        title: '',
        content: '验证不能为空或格式不对',
      })
      return;
    }
    var adds = '';
    if (that.data.auth == 0) {
      this.setData({
        gousign: 2
      })
      adds = "&type=forget";
    }
    if(that.data.gousign == 1){
      wx.showModal({
        title: '',
        content: '请先同意用户协议',
      })
      return;
    }
    
    // 验证短信
    // 获取时间戳
    var apinameIsMeb = "/Action/PK01/SMSVerifyCheck";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        mobile: this.data.phone,
        code: dataform.authword
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if(res.data.data == true){
          wx.navigateTo({
            url: '/pages/makesure/makesure?phone=' + dataform.phone + adds ,
          })
        }else{
          wx.showModal({
            title: '',
            content: '验证码错误，请重试~',
          })
          return;
        }
      }
    })

    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    
  },
  getphone:function(e){
    this.setData({
      phone: e.detail.value
    })
    // console.log(e.detail.value);
  },
  toprotocol:function(){
    wx.navigateTo({
      url: '/pages/protocol/protocol',
    })
  }
})